package com.culture.config.controller;

import java.util.List;

import javax.validation.Valid;

import com.culture.config.common.model.ResponseData;
import com.culture.config.common.util.ResponseUtil;
import com.culture.config.model.po.*;
import com.culture.config.model.vo.*;
import com.culture.config.service.JobLogService;
import com.culture.config.service.JobService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api
@RestController
@RequestMapping({"/api/job"})
public class JobController extends BaseController{
    @Autowired
    private JobService jobService;
    @Autowired
    private JobLogService jobLogService;

    @ApiOperation("page job")
	@PreAuthorize(value = "hasRole('ROLE_admin')")
	@PostMapping("/page")
    public ResponseData<List<JobVO>> page(@RequestBody @Valid JobPagePO param){
        this.authenticationValidate();
        return ResponseUtil.successPage(this.jobService.page(param));
    }

    @ApiOperation("add job")
	@PreAuthorize(value = "hasRole('ROLE_root')")
	@PostMapping("/add")
    public ResponseData<String> add(@RequestBody @Valid JobAddPO jobAddPO){
        this.authenticationValidate();
        this.jobService.add(jobAddPO);
        return ResponseUtil.success();
    }

    @ApiOperation("update job")
	@PreAuthorize(value = "hasRole('ROLE_root')")
	@PostMapping("/update")
    public ResponseData<String> update(@RequestBody @Valid JobUpdatePO jobUpdatePO){
        this.authenticationValidate();
        this.jobService.update(jobUpdatePO);
        return ResponseUtil.success();
    }

    @ApiOperation("update job status")
	@PreAuthorize(value = "hasRole('ROLE_root')")
	@PostMapping("/status/update")
    public ResponseData<String> updateStatus(@RequestBody @Valid JobStatusUpdatePO jobStatusUpdatePO){
        this.authenticationValidate();
        this.jobService.updateStatus(jobStatusUpdatePO);
        return ResponseUtil.success();
    }    

    @ApiOperation("schedule job")
	@PreAuthorize(value = "hasRole('ROLE_admin')")
	@PostMapping("/schedule")
    public ResponseData<String> schedule(@RequestBody @Valid JobSchedulePO jobSchedulePO){
        this.authenticationValidate();
        return ResponseUtil.success(this.jobService.schedule(jobSchedulePO));
    }


    @ApiOperation("page job log")
	@PreAuthorize(value = "hasRole('ROLE_admin')")
	@PostMapping("/log/page")
    public ResponseData<List<JobLogVO>> logPage(@RequestBody @Valid JobLogPagePO jobLogPagePO){
        this.authenticationValidate();
        return ResponseUtil.successPage(this.jobLogService.page(jobLogPagePO));
    }

}
